package com.example.api.controller;

import com.example.api.dto.QuestionDTO;
import com.example.api.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    // 获取所有问题
    @GetMapping
    public ResponseEntity<List<QuestionDTO>> getAllQuestions() {
        List<QuestionDTO> questions = questionService.getAllQuestions();
        return new ResponseEntity<>(questions, HttpStatus.OK);
    }

    // 根据类别ID获取问题
    @GetMapping("/by-category")
    public ResponseEntity<List<QuestionDTO>> getQuestionsByCategoryId(@RequestParam String categoryId) {
        List<QuestionDTO> questions = questionService.getQuestionsByCategoryId(categoryId);
        return new ResponseEntity<>(questions, HttpStatus.OK);
    }

    // 添加题目
    @PostMapping
    public ResponseEntity<QuestionDTO> addQuestion(@RequestBody QuestionDTO questionDTO) {
        questionService.addQuestion(questionDTO);
        return new ResponseEntity<>(questionDTO, HttpStatus.CREATED);
    }

    // 删除题目
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteQuestion(@PathVariable Integer id) {
        questionService.deleteQuestion(id);
        return ResponseEntity.noContent().build();
    }

    // 修改题目
    @PutMapping("/{id}")
    public ResponseEntity<QuestionDTO> updateQuestion(@PathVariable Integer id, @RequestBody QuestionDTO questionDTO) {
        questionService.updateQuestion(id, questionDTO);
        return new ResponseEntity<>(questionDTO, HttpStatus.OK);
    }
}